<?php
namespace app\index\controller;
use think\Db;
use think\Request;
use think\Url;
use think\Session;
use think\Controller;
use app\index\model\Webconfig as thisModel;
 /**
 * 
 */
class Config extends Base
{
	
	public function index()
	{
		$class = DB::name('webtype')->where('id <> 5')->order('orderid asc')->select();
		foreach ($class as $k => $v) {
			$class[$k]['data'] = DB::name('webconfig')->where('vargroup = '.$v['id'])->select();
		}
		return $this->fetch('index',[
			'class' => $class,
		]);
	}
	
	public function getInfo()
	{
		if(Request::instance()->isPost()){
			$id = Request::instance()->post('id');
			$res = DB::name('webconfig')->where('id = '.$id)->find();
			if($res){
			$res['success'] = 1;	
			}else{
			$res['success'] = 0;
			}
			return json($res);
		}
	}

	public function add()
	{
		if(Request::instance()->isPost()){
			$data = Request::instance()->Post();
			$data['varname'] = 'web_'.$data['varname'];
			$data['orderid'] = thisModel::getOrderid($data['vargroup']);
			$res = thisModel::add($data);
			if(!empty($res)){
				$value = DB::name('webconfig')->field('varname,varvalue')->select();
				$arr = [];
				foreach ($value as $k => $v) {
					$arr[$v['varname']] = $v['varname'];
				}
				// 文件路径
                $path = APP_PATH . "common" . DS . "common" . DS . "web.inc.php";
                file_put_contents($path, serialize($arr));
                $this->success('新增成功','index/config/index'); 
			}

		}
	}

	public function update()
	{
		if(Request::instance()->isPost()){
			$data = Request::instance()->post();
		}
	}

}
?>